W14. Shortest Path Algorithms
1. Theory
1.1 Shortest-Path Problem Family
1.1.1 Core Problem and Applications
Given a weighted directed graph
The shortest-path weight from
A shortest path is any path
1.1.2 Standard Variants
The lecture distinguishes several standard shortest-path variants, and the choice of algorithm depends on which variant is required.
- Single-pair shortest path: compute a shortest path from one source
to one target . - Single-source shortest paths (SSSP): compute shortest paths from one fixed source
to every vertex. - Single-destination shortest paths: compute shortest paths from every vertex to one fixed destination
. - All-pairs shortest paths (APSP): compute shortest paths for every ordered pair
.
The single-destination problem reduces to single-source by reversing every edge. In the reversed graph, shortest paths from all vertices to
1.2 Optimal Substructure, Negative Cycles, and Relaxation
1.2.1 Optimal Substructure
Shortest paths have the crucial property of optimal substructure. If
is a shortest path from
is itself a shortest path from
This idea is the reason both greedy algorithms and dynamic-programming algorithms work for shortest paths. Dijkstra relies on it locally, while Floyd–Warshall relies on it through a recurrence over restricted intermediate vertices.
1.2.2 Negative Edges and Negative Cycles
Negative edge weights do not by themselves make the shortest-path problem meaningless. A graph may contain negative edges and still have perfectly well-defined shortest paths. The real obstacle is a reachable negative-weight cycle.
If a path from
So the fundamental distinction is:
- negative edges without reachable negative cycles: shortest paths are still well defined;
- reachable negative cycles: shortest-path weights may fail to exist as finite minima.
1.2.3 Relaxation
The three main algorithms in this lecture are all built around relaxation. In single-source problems, each vertex
- a distance estimate
, which is an upper bound on the true distance ; - a predecessor
, which records the previous vertex on the current best-known path.
The primitive operation is:
Each relaxation either improves an estimate or leaves it unchanged. The algorithms differ mainly in the order in which they perform these relaxations:
- Dijkstra chooses the next vertex greedily;
- Bellman–Ford repeatedly relaxes every edge;
- Floyd–Warshall relaxes all ordered pairs through progressively allowed intermediate vertices.
1.3 Dijkstra’s Algorithm
1.3.1 Greedy Idea and Relation to Prim
Dijkstra’s algorithm solves the single-source shortest-path problem when all edge weights are nonnegative. It resembles Prim’s minimum-spanning-tree algorithm very closely:
- both algorithms maintain a priority queue of vertices;
- both repeatedly extract one vertex with minimum key;
- both update neighboring vertices through decrease-key operations.
The difference lies in the meaning of the key:
- in Prim, the key of
is the cheapest edge connecting to the current tree; - in Dijkstra, the key of
is the current best known source-to- distance estimate.
The greedy invariant is stronger in Dijkstra: once a vertex
This is correct only because all edge weights are nonnegative. Nonnegativity guarantees that any alternative path reaching
1.3.2 Pseudocode Structure
The CLRS-style structure is:
- Initialize all vertices with distance
and predecessorNIL, except the source with distance . - Insert all vertices into a min-priority queue keyed by
. - Repeatedly extract the vertex with minimum estimate.
- Relax all outgoing edges of that extracted vertex.
After termination, the predecessor pointers form a shortest-path tree rooted at the source, provided all weights are nonnegative.
1.3.3 Running Time
With adjacency lists and a binary heap:
EXTRACT-MINis performed times at each;DECREASE-KEYis performed at most times at each;- scanning adjacency lists contributes
total.
Therefore the total time is
With Fibonacci heaps:
EXTRACT-MINcosts amortized;DECREASE-KEYcosts amortized;
so the total time becomes
The lecture also emphasizes the
For dense graphs with
1.3.4 Why Dijkstra Fails on Negative Edges
The correctness proof of Dijkstra depends on the claim that once a vertex is extracted, it can never be improved later. A negative edge destroys exactly this claim. A path discovered afterward may enter a not-yet-processed region of the graph and then return through a negative edge, creating a smaller value for a vertex that Dijkstra already finalized.
This is why the problem is not merely a technicality: with negative weights, the algorithm’s core greedy step is no longer safe.
1.4 Bellman–Ford Algorithm
1.4.1 Fundamental Observation
Any simple path in a graph with
This means that to compute all shortest paths from a source, it is enough to ensure correctness for paths using at most
1.4.2 Algorithm and Negative-Cycle Detection
Bellman–Ford implements this observation directly:
- initialize all estimates;
- repeat
times: relax every edge in the graph; - perform one extra pass over all edges;
- if any estimate still decreases, report a reachable negative cycle.
The logic is clean. After one full pass, all shortest paths using at most one edge are correct. After two passes, all shortest paths using at most two edges are correct. After
The extra pass is not for computing distances. It is a certificate check: if some edge still relaxes, then some path using at least
1.4.3 Complexity and Use Cases
Each pass examines all edges once, which costs
This is asymptotically slower than Dijkstra on nonnegative graphs, but Bellman–Ford is the correct tool when:
- negative edge weights may occur;
- reachable negative cycles must be detected explicitly;
- a simple, uniform edge-relaxation procedure is preferable to a greedy queue discipline.
1.5 Properties of Shortest-Path Estimates
The lecture isolates several properties that appear in correctness proofs. These are worth studying because they explain why relaxation-based algorithms converge.
1.5.1 Triangle Inequality and Upper Bounds
For any edge
This is the triangle inequality for shortest paths: going from
At the same time, relaxation algorithms preserve the upper-bound property:
at all times. Estimates may be too large, but they are never too small.
1.5.2 No-Path, Convergence, and Path Relaxation
If no path from
The convergence property says that if
The path-relaxation property generalizes this. If the edges of a shortest path are relaxed in the path’s order, one after another, then the destination estimate eventually becomes exact. Bellman–Ford exploits this over repeated full passes, and Dijkstra exploits it through the fact that nonnegative edges let exactness spread outward in increasing-distance order.
1.6 Floyd–Warshall Algorithm
1.6.1 All-Pairs Perspective
The all-pairs shortest-path problem asks for
Unlike Dijkstra and Bellman–Ford, which are source-centered, Floyd–Warshall is pair-centered. It asks how the best route from
1.6.2 Dynamic-Programming State
Number the vertices as
to be the weight of the shortest path from
This is the right subproblem family because shortest paths are naturally built from intermediate vertices, not from a fixed number of edges. A shortest path may have many edges, but what matters for the recurrence is whether it is allowed to pass through the pivot vertex
1.6.3 Recurrence and Loop Order
For each stage
This recurrence has a direct interpretation:
- either the best path from
to does not use vertex as an intermediate vertex, so the old value remains best; - or the best path does use
, in which case it splits into a shortest path from to and a shortest path from to , both using only vertices up to internally.
The outermost loop must be over
1.6.4 Complexity, In-Place Updates, and Negative Cycles
The algorithm performs three nested loops over the vertices, so its running time is
The distance matrix uses
space, and storing predecessors requires another
The lecture also notes two important implementation facts:
- the algorithm can be implemented in place, overwriting the distance matrix entry by entry, but this requires careful reasoning about dependencies;
- if the final matrix has some diagonal entry
, then the graph contains a negative-weight cycle through vertex .
1.7 Choosing the Right Algorithm
For the three algorithms in this lecture, the main selection rule is:
- use Dijkstra for single-source shortest paths when all edge weights are nonnegative;
- use Bellman–Ford for single-source shortest paths when negative edges may appear or when reachable negative cycles must be detected;
- use Floyd–Warshall when a full all-pairs distance matrix is required.
All three are built from the same conceptual core — relaxation and optimal substructure — but they organize the computation in very different ways.
2. Definitions
- Shortest-path weight
: The minimum total weight of a path from to , or if no such path exists. - Shortest path: A path whose total weight equals the shortest-path weight between its endpoints.
- Single-source shortest paths (SSSP): The problem of computing distances from one source vertex to all vertices.
- All-pairs shortest paths (APSP): The problem of computing distances for all ordered pairs of vertices.
- Distance estimate
: The algorithm’s current upper bound on the true shortest-path distance from the source to . - Predecessor
: The previous vertex on the current best known path to . - Relaxation: The update step that replaces
by when that value is smaller. - Optimal substructure: The property that every subpath of a shortest path is itself shortest.
- Negative-weight edge: An edge with weight less than
. - Negative-weight cycle: A directed cycle whose total edge weight is negative.
- Triangle inequality: For every edge
, . - Upper-bound property: During relaxation algorithms, estimates always satisfy
. - No-path property: If
is unreachable from the source, then remains . - Convergence property: If
is exact and lies on a shortest path, then relaxing makes exact. - Path-relaxation property: Relaxing the edges of a shortest path in order eventually makes the destination estimate exact.
- Dijkstra’s algorithm: A greedy single-source shortest-path algorithm correct for nonnegative edge weights.
- Bellman–Ford algorithm: A single-source shortest-path algorithm that handles negative edges and detects reachable negative cycles.
- Floyd–Warshall algorithm: A dynamic-programming algorithm for all-pairs shortest paths based on allowed intermediate vertices.
3. Formulas
- Shortest-path definition:
- Relaxation update: If
, then and - Triangle inequality:
- Dijkstra with binary heap:
- Dijkstra with Fibonacci heap:
- Dijkstra with
-ary heap: - Bellman–Ford running time:
- Floyd–Warshall recurrence:
- Floyd–Warshall running time:
- Floyd–Warshall space usage:
4. Practice
4.1. Run Dijkstra from Vertex A (Lecture 12, Task 1)
Run Dijkstra’s algorithm on the directed graph with edges
starting from vertex
Click to see the solution
Key Concept: Dijkstra repeatedly finalizes the vertex with the smallest current estimate. Because all edge weights are nonnegative, once a vertex is extracted its estimate is final.
Initialize:
We also set every predecessor to NIL.
| Step | Extracted vertex | Successful relaxations | Distances after the step |
|---|---|---|---|
| 0 | — | initialize only | |
| 1 | |||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | none | ||
| 6 | none | final |
Now check the nontrivial relaxations explicitly.
- From
: - From
: The edge gives , which does not improve . - From
: - From
: The candidate value for is , which is worse than . - From
: so nothing improves.
The final shortest-path distances are:
The predecessor pointers describe the shortest-path tree:
So one set of shortest paths is:
Answer: The final distances are
4.2. Explain Why Dijkstra Fails with Negative Edge (Lecture 12, Task 2)
Run Dijkstra’s algorithm on the lecture graph with a negative edge and explain why the algorithm can produce an incorrect result.
Click to see the solution
Key Concept: Dijkstra assumes that once a vertex leaves the priority queue, its estimate can never improve again. Negative edges break exactly that assumption.
Suppose a vertex
where the edge
may become smaller than the supposedly final value of
At that point Dijkstra has no repair mechanism, because extracted vertices are never returned to the queue. The algorithm therefore locks in a value that may later turn out to be too large.
So the failure is not accidental. It is structural:
- Dijkstra finalizes vertices permanently.
- Negative edges allow later improvements to earlier vertices.
- Therefore the greedy invariant is false.
This is why Dijkstra is valid only when all edge weights are nonnegative.
Answer: Dijkstra fails because a later path through the negative edge
4.3. Trace Dijkstra from Vertex L on the Large Lecture Graph (Lecture 12, Task 3)
Run Dijkstra’s algorithm on the large graph from the lecture slide, starting from vertex
Click to see the solution
Key Concept: On a large graph, Dijkstra is still the same repeated routine: extract the smallest tentative distance, relax all outgoing edges, and record each predecessor update.
The slide image does not list every edge textually, so the most reliable self-study solution from the available source is the exact procedure to apply to the original diagram.
- Initialize:
and every other distance is . - Insert all vertices into a min-priority queue keyed by their tentative distance.
- Repeatedly:
- extract the vertex
with minimum tentative distance; - mark
as finalized; - for every outgoing edge
, perform relaxation.
- extract the vertex
- Continue until the queue becomes empty.
While tracing the algorithm on paper, maintain a table with:
- current queue minimum,
- current distances,
- predecessor changes,
- the set of finalized vertices.
Three correctness checks help you catch mistakes:
- Extracted distances must be nondecreasing.
- Every predecessor update must satisfy
- The predecessor pointers at the end must form a tree rooted at
.
So, even though the OCR transcript does not preserve the full edge list, the solving method is completely determined: it is the standard Dijkstra trace used in Task 4.1, only on a larger graph.
Answer: Use the same Dijkstra trace as in Task 4.1, starting from
4.4. Run Bellman-Ford from Source (Lecture 12, Task 4)
Run Bellman–Ford on the 5-vertex lecture graph with source
Click to see the solution
Key Concept: After pass
Initialize:
We now perform
| Pass | Distance estimates after the pass |
|---|---|
| 0 | |
| 1 | |
| 2 | |
| 3 | |
| 4 |
Now justify the important updates.
Pass 1
Only the outgoing edges of
Pass 2
Using the new value of
Then using the new value of
which improves the old value
Then using the new value of
but this particular improvement appears cleanly only after one more full pass because Bellman–Ford propagates information edge by edge according to the scan order.
Pass 3
The value of
No further edge can improve any estimate.
So the final shortest-path distances from
In the extra negative-cycle check pass, no edge relaxes further, so there is no reachable negative cycle in this graph.
Answer: The final Bellman-Ford distances from
4.5. Detect a Negative Cycle with Bellman-Ford (Lecture 12, Task 5)
Use Bellman–Ford to explain why the lecture’s second 5-vertex graph contains a reachable negative cycle.
Click to see the solution
Key Concept: If Bellman-Ford can still improve some estimate after
Bellman–Ford detects a reachable negative cycle by performing one extra full pass after the usual
The lecture trace shows exactly the warning sign we are looking for: the distance estimates continue decreasing from one iteration to the next instead of stabilizing. In particular, the handwritten states keep improving across later passes, which means the algorithm is repeatedly finding cheaper walks.
That behavior has only one explanation:
- a walk is using more than
edges, - yet it is still improving the estimate,
- therefore that walk must repeat a vertex,
- and the repeated cycle must have negative total weight.
So the graph contains a reachable negative-weight cycle. For every vertex reachable from that cycle, the shortest-path value is not a finite minimum, because looping around the cycle one more time always produces a cheaper path.
Answer: The graph contains a reachable negative-weight cycle, because the Bellman-Ford estimates keep decreasing even after the normal
4.6. Prove Why Passes Suffice (Lecture 12, Task 6)
Let
Click to see the solution
Key Concept: In the absence of reachable negative cycles, every shortest path can be chosen simple.
- A simple path in a graph with
vertices uses at most edges. - After one Bellman–Ford pass, all shortest paths using at most one edge are correctly represented.
- After two passes, all shortest paths using at most two edges are correctly represented.
- By induction, after
passes, all shortest paths using at most edges are correctly represented.
Now take any vertex
Thus
Answer:
4.7. Modify Bellman-Ford for the Bound (Lecture 12, Task 7)
Suppose every shortest path from
Click to see the solution
Key Concept: If every shortest path uses at most
If every shortest path uses at most
The modification is:
- perform at most
ordinary passes over all edges; - keep a Boolean variable
changed; - during each pass, set
changed = trueif some relaxation succeeds; - if a pass ends with
changed = false, stop early because all estimates have already converged; - after the ordinary passes, do one additional pass for negative-cycle detection.
This gives at most
Why is it correct? Because after pass
Answer: Replace the usual
4.8. Run Floyd-Warshall on the 5-Vertex Graph (Lecture 12, Task 8)
Run Floyd–Warshall on the 5-vertex lecture graph. Use the vertex order
Click to see the solution
Key Concept: Floyd-Warshall gradually enlarges the set of allowed intermediate vertices; after stage
We start from the initial weight matrix
Initial matrix
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 6 | 7 | ||
| B | 0 | 5 | 8 | -4 | |
| C | -2 | 0 | |||
| D | -3 | 0 | 9 | ||
| E | 2 | 7 | 0 |
Now process vertices one by one as allowed intermediate vertices.
After allowing
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 6 | 7 | ||
| B | 0 | 5 | 8 | -4 | |
| C | -2 | 0 | |||
| D | -3 | 0 | 9 | ||
| E | 2 | 8 | 7 | 9 | 0 |
Only the paths
After allowing
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 6 | 11 | 7 | 2 |
| B | 0 | 5 | 8 | -4 | |
| C | -2 | 0 | 6 | -6 | |
| D | -3 | 0 | 9 | ||
| E | 2 | 8 | 7 | 9 | 0 |
The main updates are:
After allowing
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 6 | 11 | 7 | 2 |
| B | 0 | 5 | 8 | -4 | |
| C | -2 | 0 | 6 | -6 | |
| D | -5 | -3 | 0 | -9 | |
| E | 2 | 5 | 7 | 9 | 0 |
Important updates:
After allowing
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 2 | 4 | 7 | -2 |
| B | 0 | 5 | 8 | -4 | |
| C | -2 | 0 | 6 | -6 | |
| D | -5 | -3 | 0 | -9 | |
| E | 2 | 4 | 6 | 9 | 0 |
Important updates:
After allowing
| A | B | C | D | E | |
|---|---|---|---|---|---|
| A | 0 | 2 | 4 | 7 | -2 |
| B | -2 | 0 | 2 | 5 | -4 |
| C | -4 | -2 | 0 | 3 | -6 |
| D | -7 | -5 | -3 | 0 | -9 |
| E | 2 | 4 | 6 | 9 | 0 |
These last improvements come from routes that pass through
The final all-pairs distance matrix is therefore
All diagonal entries are
Answer: The final all-pairs distance matrix is